﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.Linq;
using System.Linq;
using DevExpress.XtraReports;
using DevExpress.XtraReports.UI;

namespace Exact_Extension
{
    public partial class InitializeOTHFInvoice : DevExpress.XtraEditors.XtraForm
    {
        ExactDataContext DS;
        Common common;
        IFormatProvider culture = new System.Globalization.CultureInfo("fr-FR", true);
        DateTime EnrollmentDate = DateTime.Now;
        int LastRowHandleInvoice = 0;
        int LastRowHandleStudent = 0;
      
        public InitializeOTHFInvoice(ExactDataContext _DS)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
            
            EnrollmentDate = Convert.ToDateTime("01/08/" + DateTime.Now.Year.ToString(), culture);
           
            
        }
        public InitializeOTHFInvoice(ExactDataContext _DS,List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {
            InitializeComponent();
            DS = _DS;
            common = new Common(DS);
            this.LoadDataFromAnotherForm(ListStudent);
            EnrollmentDate = Convert.ToDateTime("01/08/" + DateTime.Now.Year.ToString(), culture);

        }
        private void LoadDataInitialize()
        {
      
            this.dateEdit_InvoiceDate.EditValue = DateTime.Now.Date;
            this.dateEdit_DueDate.EditValue = DateTime.Now.Date;


            var querySCHY = from SchoolYear in DS.ISHCMC_SchoolYears
                            select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySCHY;

            if (querySCHY.Count() > 0)
            {
                this.lookUpEdit_SchoolYear.EditValue = querySCHY.First().SchoolYear;
            }

            var querySchool = from School in DS.ISHCMC_V_Schools
                              select School;
            this.iSHCMCVSchoolBindingSource.DataSource = querySchool;
            if (querySchool.Count() > 0)
            {
                this.lookUpEdit_School.EditValue = querySchool.First().kstplcode;
            }

            var queryItem = from IT in DS.Items
                            where IT.Assortment == 0
                            select IT;
            this.itemBindingSource.DataSource = queryItem;
            if (queryItem.Count() > 0)
            {
                this.cbxItem.EditValue = queryItem.First().ItemCode;
            }
            
        }

        private void LoadData()
        {

         
            string school = "";
         
            if (this.lookUpEdit_School.EditValue != null)
            {
                school = this.lookUpEdit_School.EditValue.ToString();
            }
            
            var queryStudent = from ListStudent in DS.ISHCMC_V_StudentListWithDebtors
                               where ListStudent.School == school
                               select ListStudent;

            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = queryStudent;
            

          
           
            if (queryStudent.Count() > 0)
            {
                this.gridView_Student.SelectRow(0);
                this.gridView_Student.FocusedRowHandle = 0;
            }

         
        }
        private void LoadAllInvoice()
        {
            
            var queryInvoice = from ListInvoice in DS.ISHCMC_Invoices
                               join  ListItem in DS.Items
                               on  ListInvoice.Itemcode equals ListItem.ItemCode
                               where ListInvoice.Status == 0 && ListItem.Assortment ==0
                               select ListInvoice;



            this.iSHCMCInvoiceBindingSource.DataSource = queryInvoice;
         
        }
        private void LoadDataPerInvoiceHeader(int InvoiceHeaderID)
        {
            var queryInvoice = from ListInvoice in DS.ISHCMC_Invoices
                               join ListItem in DS.Items
                              on ListInvoice.Itemcode equals ListItem.ItemCode
                               where ListInvoice.Status == 0 && ListInvoice.InvoiceHeaderID == InvoiceHeaderID  && ListItem.Assortment ==0
                               select ListInvoice;



            this.iSHCMCInvoiceBindingSource.DataSource = queryInvoice;
        }
        private void LoadDataPerStudent()
        {
            if (LastRowHandleStudent >= 0)
            {
                ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(LastRowHandleStudent);
                List<ISHCMC_Invoice> CollectionInvoice = new List<ISHCMC_Invoice>();
                var queryInvoice = from ListInvoice in DS.ISHCMC_Invoices
                                   join ListItem in DS.Items
                                    on ListInvoice.Itemcode equals ListItem.ItemCode
                                   where ListInvoice.Status == 0 && ListInvoice.StudentID == itemStudent.StudentID && ListItem.Assortment == 0
                                   select ListInvoice;
                foreach (ISHCMC_Invoice item in queryInvoice)
                {

                    CollectionInvoice.Add(item);

                }

                this.iSHCMCInvoiceBindingSource.DataSource = CollectionInvoice;
            }
           
        }
        private void LoadDataFromAnotherForm(List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {

            this.dateEdit_InvoiceDate.EditValue = DateTime.Now.Date;


            var querySCHY = from SchoolYear in DS.ISHCMC_SchoolYears
                            select SchoolYear;
            this.iSHCMCSchoolYearBindingSource.DataSource = querySCHY;

            if (querySCHY.Count() > 0)
            {
                this.lookUpEdit_SchoolYear.EditValue = querySCHY.First().SchoolYear;
            }


          

           
            this.iSHCMCVStudentListWithDebtorBindingSource.DataSource = ListStudent;



        }
        private void LoadDataPerStudentRowHandle(int FocusedRow)
        {
            if (FocusedRow >= 0)
            {
                ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(FocusedRow);
                List<ISHCMC_Invoice> CollectionInvoice = new List<ISHCMC_Invoice>();
                var queryInvoice = from ListInvoice in DS.ISHCMC_Invoices
                                   join ListItem in DS.Items
                                   on ListInvoice.Itemcode equals ListItem.ItemCode
                                   where ListInvoice.Status == 0 && ListInvoice.StudentID == itemStudent.StudentID && ListItem.Assortment == 0
                                   select ListInvoice;
                foreach (ISHCMC_Invoice item in queryInvoice)
                {
                    ISHCMC_V_Item itemFee = DS.ISHCMC_V_Items.Single(c => c.ItemCode == item.Itemcode);
                    if (itemFee.Assortment == 0)
                    {
                        CollectionInvoice.Add(item);
                    }
                }

                this.iSHCMCInvoiceBindingSource.DataSource = CollectionInvoice;
            }
        }
        private void LoadListInvoice(List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {
            List<ISHCMC_Invoice> CollectionInvoice = new List<ISHCMC_Invoice>();

            foreach (ISHCMC_V_StudentListWithDebtor itemStudent in ListStudent)
            {
                var queryInvoice = from ListInvoice in DS.ISHCMC_Invoices
                                   join ListItem in DS.Items
                                   on ListInvoice.Itemcode equals ListItem.ItemCode
                                   where ListInvoice.Status == 0 && ListInvoice.StudentID == itemStudent.StudentID && ListItem.Assortment == 0
                                   select ListInvoice;
                foreach (ISHCMC_Invoice item in queryInvoice)
                {
                   
                        CollectionInvoice.Add(item);
                
                }
                
            }



            this.iSHCMCInvoiceBindingSource.DataSource = CollectionInvoice;
        }
        private ISHCMC_ListInvoiceDetail_DebitNote GetInvoiceDetail(ISHCMC_Invoice Inv, string itemcode)
        {

            ISHCMC_V_StudentListWithDebtor student = DS.ISHCMC_V_StudentListWithDebtors.Single(c => c.StudentID == Inv.StudentID);
            cicmpy studentInExact = DS.cicmpies.Single(c => c.cmp_code == student.DebtorCode);
            cicmpy parentaccount = DS.cicmpies.Single(c => c.debnr == studentInExact.InvoiceDebtor);
            cicmpy invoicedebtor = DS.cicmpies.Single(c => c.debnr == studentInExact.InvoiceDebtor);
            ISHCMC_ListInvoiceDetail_DebitNote InvDetail = new ISHCMC_ListInvoiceDetail_DebitNote();
            if (studentInExact.InvoiceDebtor == null)
            {
                InvDetail.BillingName = parentaccount.cmp_name;
                InvDetail.BillingAddr1 = parentaccount.cmp_fadd1;
                InvDetail.BillingAddr2 = parentaccount.cmp_fadd2;
                InvDetail.BillingAddr3 = parentaccount.cmp_fadd3;
            }
            else
            {
                InvDetail.BillingName = invoicedebtor.cmp_name;
                InvDetail.BillingAddr1 = invoicedebtor.cmp_fadd1;
                InvDetail.BillingAddr2 = invoicedebtor.cmp_fadd2;
                InvDetail.BillingAddr3 = invoicedebtor.cmp_fadd3;
            }
            if (studentInExact.TextField7 != null)
            {
                InvDetail.BillingAddr1 = studentInExact.TextField7;
                if (studentInExact.TextField7.Length > 80)
                {
                    InvDetail.BillingAddr2 = studentInExact.TextField8;

                }
                if (studentInExact.TextField7.Length > 160)
                {
                    InvDetail.BillingAddr3 = studentInExact.TextField9;
                }
            }
            ISHCMC_InvoiceHeader InvHeader = common.GetInvoiceHeader(student.StudentID, Inv.SchoolYear,student.School);
            InvDetail.CurrentGrade = student.Grade;
            InvDetail.DueDate = Inv.DueDate;
            InvDetail.StudentID = Inv.StudentID;
            InvDetail.StudentName = studentInExact.cmp_name;

            InvDetail.yourref = Inv.InvoiceNo;
            InvDetail.Invoicedate = Inv.TransDate;
            InvDetail.EnrolmentDate = student.EnrollmentDate;
            InvDetail.description = Inv.Section;
            InvDetail.itemcode = Inv.Itemcode;
            InvDetail.Discount = Convert.ToDouble(Inv.Discount);
            InvDetail.Price = Convert.ToDouble(Inv.Amount);
            InvDetail.NetPrice = InvDetail.Price - InvDetail.Price * (InvDetail.Discount / 100);
            return InvDetail;
        }

       

        private void gridView_Invoice_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                              this.popupMenu_GVInvoice.ShowPopup(Control.MousePosition);
                              LastRowHandleInvoice = e.RowHandle;
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
          
        
           
                ISHCMC_Invoice itemInv = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);
                var queryInv = from Inv in DS.ISHCMC_V_Invoices
                               where Inv.ID == itemInv.ID
                               && Inv.Fulfilled == "Fulfilled"
                               select Inv;
                if (queryInv.Count() == 0)
                {
                    MsgDeleteInvoice msgDelInv = new MsgDeleteInvoice(itemInv, common);
                    msgDelInv.StartPosition = FormStartPosition.CenterScreen;
                    DialogResult result = msgDelInv.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        this.gridView_Student.SelectRow(LastRowHandleStudent);
                        this.LoadDataPerStudent();
                    }
                    if (result == DialogResult.No)
                    {
                        MessageBox.Show("Can not delete this invoice , please you check with administrator again !!");
                    }
                }
                else
                {
                    MessageBox.Show("Invoice already fulfilled!!!");
                }
           
        }

        private void gridView_Student_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            LastRowHandleStudent = e.RowHandle;
            if (e.Button == MouseButtons.Right)
            {
                this.popupMenu_GVStudent.ShowPopup(Control.MousePosition);
             
            }
            if (e.Button == MouseButtons.Left)
            {
                this.LoadDataPerStudent();
            }
            if (e.Clicks == 2)
            {

                ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(e.RowHandle);
                var queryInvHeader = from InvoiceH in DS.ISHCMC_InvoiceHeaders
                                     where InvoiceH.Debcode == student.DebtorCode
                                     && InvoiceH.StudentID == student.StudentID
                                     && InvoiceH.SchoolYear == this.lookUpEdit_SchoolYear.EditValue.ToString()
                                     select InvoiceH;
                if (queryInvHeader.Count() > 0)
                {
                    ISHCMC_InvoiceHeader itemInvH = queryInvHeader.First();

                    InvoiceDetail inv = new InvoiceDetail(DS, itemInvH);
                    inv.ShowDialog();
                }
                else
                {
                    MessageBox.Show("This student haven't generate invoice yet");
                }
            }
        }

        private void btnEditDetail_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(LastRowHandleStudent);

            MsgEditStudent msgEditStudent = new MsgEditStudent(DS);
            msgEditStudent.PopulateControl(itemStudent);
            DialogResult result  = msgEditStudent.ShowDialog();
            if (result == DialogResult.OK)
            {
                this.LoadData();
                msgEditStudent.Dispose();
            }
            if(result == DialogResult.No)
            {
                MessageBox.Show("Update Student Information occured error, please contact Administrator!!!!");

                msgEditStudent.Dispose();
            }

        }

        private void gridView_Student_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
          this.LoadDataPerStudentRowHandle(e.FocusedRowHandle);
          
        }

        private void btnAttachment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);

            InvoiceAttach InvAttach = new InvoiceAttach(DS, InvoiceDetail);
            InvAttach.Text = InvAttach.Text + " Invoice no :" + InvoiceDetail.ProformaInvoiceNo + " for Student Code : " + InvoiceDetail.StudentID;
            InvAttach.Show();
        }

        private void btnLogBook_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);

            InvoiceHistory InvHistory = new InvoiceHistory(DS, InvoiceDetail);
            InvHistory.Text = InvHistory.Text + " Invoice no :" + InvoiceDetail.ProformaInvoiceNo + " for Student Code : " + InvoiceDetail.StudentID;
            InvHistory.Show();
        }

    
     
      

        private void btnSpecialCase_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);
            if (InvoiceDetail.Itemcode == "TF" || InvoiceDetail.Itemcode == "ADF")
            {
                MsgSpecialCase msg = new MsgSpecialCase(DS, InvoiceDetail);
               
                DialogResult result = msg.ShowDialog();
                
                if (result == DialogResult.OK)
                {
                    MessageBox.Show("You already special discount sucessfully , please go to Authorize Discount");
                }
                else if (result == DialogResult.No)
                {
                    MessageBox.Show("Occured some error in system , please contact administartor!!");
                }
            }
            else
            {
                MessageBox.Show("Only Tuition Fee & ADF allowed to setup Discount , you selected another fee to setup discount , system not allowed!!!");
            }
        }

        private void btnAuthorizeSP_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);
            if (InvoiceDetail.SpecialDiscount == true)
            {
                SpecialDiscountAuthorize SPDAFrm = new SpecialDiscountAuthorize(DS, InvoiceDetail);
                SPDAFrm.Show();
            }
            else
            {
                MessageBox.Show("This invoice is not Special case!!");
            }
        }

        private void btnPayment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice InvoiceDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);

            MsgPayment Payment = new MsgPayment(InvoiceDetail,DS);
            Payment.Text = Payment.Text + " Invoice no :" + InvoiceDetail.ProformaInvoiceNo + " for Student Code : " + InvoiceDetail.StudentID;
            DialogResult result = Payment.ShowDialog();
            if (result == DialogResult.OK)
            {
                MessageBox.Show("Payment record sucessfully");
            }
            if (result == DialogResult.No)
            {
                MessageBox.Show("Occured error ,please contact administrator");

            }
        }

        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadDataInitialize();
            this.LoadData();
           
        }

        private void btnInitialize_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.lookUpEdit_SchoolYear.EditValue != null && this.dateEdit_InvoiceDate.EditValue != null)
            {
                string schoolyear = this.lookUpEdit_SchoolYear.EditValue.ToString();
                DateTime InvoiceDate = Convert.ToDateTime(this.dateEdit_InvoiceDate.EditValue);
                DateTime DueDate = Convert.ToDateTime(this.dateEdit_DueDate.EditValue);
                List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
  
           
                foreach (int i in this.gridView_Student.GetSelectedRows())
                {


                    ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                    ISHCMC_Section itemSec = new ISHCMC_Section();
                    if (itemStudent.PaymentMethod == null)
                    {
                        itemSec = common.GetSection("A", schoolyear, itemStudent.School, DueDate);
                    }
                    else
                    {
                        itemSec = common.GetSection(itemStudent.PaymentMethod, schoolyear, itemStudent.School, DueDate);
                    }
                    if (itemStudent.StudentID != null)
                    {

                        if (this.txtPrice.EditValue != null && this.cbxItem.EditValue !=null)
                        {

                            common.GenerateOTHInvoice(this.cbxItem.EditValue.ToString(), itemStudent, schoolyear, InvoiceDate, itemSec
                                , Convert.ToDecimal(this.txtPrice.EditValue),this.txtItemDescription.EditValue.ToString());
                        }
                        else
                        {
                            MessageBox.Show("Sales Price of this item is not value , please check it again ");
                        }
                       
                       
                        ListStudent.Add(itemStudent);
                    }

                }
                this.LoadListInvoice(ListStudent);
            }
            else
            {
                MessageBox.Show("Please Choose School Year & Invoice Date !!");
            }
        }

        private void btnPrintProforma_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            foreach (int i in this.gridView_Student.GetSelectedRows())
            {
                ListStudent.Add((ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i));
            }
                this.PrintProformaInv(ListStudent);
            
        }

        private void InitializeTFInvoice_Shown(object sender, EventArgs e)
        {
            this.LoadDataInitialize();
            this.LoadData();
          
        }


        private void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView_Invoice.ShowPrintPreview();
        }

        private void btnPrintPopup_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_Invoice Inv = (ISHCMC_Invoice) this.gridView_Invoice.GetRow(LastRowHandleInvoice);
            var queryST = from ST in DS.ISHCMC_V_StudentListWithDebtors
                          where ST.StudentID == Inv.StudentID
                          select ST;
            if (queryST.Count() > 0)
            {
                this.PrintProformaInv(Inv.ProformaInvoiceNo,queryST.First());
            }

        }

        private void PrintProformaInv(List<ISHCMC_V_StudentListWithDebtor> ListStudent)
        {
            if (lookUpEdit_SchoolYear.EditValue != null)
            {
                if (lookUpEdit_SchoolYear.EditValue.ToString().Length == 4)
                {
                    RootRpt rootrpt = new RootRpt();
                    rootrpt.CreateDocument();
                    string section = this.lookUpEdit_Section.EditValue.ToString();
                    foreach (ISHCMC_V_StudentListWithDebtor student in ListStudent)
                    {

                        foreach (string ProformaNo in common.GetListProformaNo(student, this.lookUpEdit_SchoolYear.EditValue.ToString(),section))
                        {
                            
                               var querySaleJrn = from  SaleJrn in DS.ISHCMC_V_SaleJournals
                                                  where SaleJrn.VoucherNo == ProformaNo
                                                  select SaleJrn;
                               if (querySaleJrn.Count() > 0)
                               {
                                   ISHCMC_V_SaleJournal itemSaleJournal = querySaleJrn.First();
                                
                                   if (itemSaleJournal.School == "ISHCMC")
                                   {
                                       ProformaInvOTHFRpt rpt = new ProformaInvOTHFRpt();
                                       rpt.SetListInvoiceDetail(itemSaleJournal);
                                      
                                       rpt.CreateDocument();
                                       rootrpt.Pages.AddRange(rpt.Pages);
                                       rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                                   }
                                   else
                                   {
                                       ProformaInvAAOTHFRpt rpt = new ProformaInvAAOTHFRpt();
                                       rpt.SetListInvoiceDetail(itemSaleJournal);
                                       rpt.CreateDocument();
                                       rootrpt.Pages.AddRange(rpt.Pages);
                                       rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                                   }



                               }

                        }
                    }
                    ReportPrintTool PrintTool = new ReportPrintTool(rootrpt);
                    PrintTool.ShowPreview();
                }
                else
                {
                    MessageBox.Show("Please choose School Year !!");
                }
            }
            else
            {
                MessageBox.Show("Please choose School Year !!");
            }
        }

        private void PrintProformaInv(string proformaNo,ISHCMC_V_StudentListWithDebtor student)
        {
            if (lookUpEdit_SchoolYear.EditValue != null)
            {
                if (lookUpEdit_SchoolYear.EditValue.ToString().Length == 4)
                {
                    RootRpt rootrpt = new RootRpt();
                    rootrpt.CreateDocument();

              

                    
                            var queryInv = from InvoiceList in DS.ISHCMC_Invoices
                                           where InvoiceList.StudentID == student.StudentID
                                           && InvoiceList.SchoolYear == lookUpEdit_SchoolYear.EditValue.ToString()
                                           && InvoiceList.Status == 0 && InvoiceList.ProformaInvoiceNo == proformaNo
                                           select InvoiceList;



                            foreach (ISHCMC_Invoice itemInv in queryInv)
                            {
                                if (itemInv.Status == 0)
                                {

                                    var querySaleJrn = from SaleJrn in DS.ISHCMC_V_SaleJournals
                                                       where SaleJrn.ID  == itemInv.ID
                                                       select SaleJrn;
                                    if (querySaleJrn.Count() > 0)
                                    {
                                        ISHCMC_V_SaleJournal itemSaleJournal = querySaleJrn.First();
                                     
                                        if (itemSaleJournal.School == "ISHCMC")
                                        {
                                            ProformaInvOTHFRpt rpt = new ProformaInvOTHFRpt();
                                            rpt.SetListInvoiceDetail(itemSaleJournal);

                                            rpt.CreateDocument();
                                            rootrpt.Pages.AddRange(rpt.Pages);
                                            rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                                        }
                                        else
                                        {
                                            ProformaInvAAOTHFRpt rpt = new ProformaInvAAOTHFRpt();
                                            rpt.SetListInvoiceDetail(itemSaleJournal);
                                            rpt.CreateDocument();
                                            rootrpt.Pages.AddRange(rpt.Pages);
                                            rootrpt.PrintingSystem.ContinuousPageNumbering = true;
                                        }



                                    }
                                }
                            }
                           

                        

                    
                    ReportPrintTool PrintTool = new ReportPrintTool(rootrpt);
                    PrintTool.ShowPreview();
                }
                else
                {
                    MessageBox.Show("Please choose School Year !!");
                }
            }
            else
            {
                MessageBox.Show("Please choose School Year !!");
            }
        }

        private void btnShowAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadAllInvoice();
        }

        private void btnExportStudent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView_Student.ShowPrintPreview();
        }

        private void btnRefreshStudent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadData();
        }

        private void btnShowAllStudent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadData();
        }

        private void btnPrintDN_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView_Invoice.GetSelectedRows().Length > 0)
            {
                ISHCMC_Invoice Inv = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(this.gridView_Invoice.GetSelectedRows()[0]);
                MsgPrintDN printfrm = new MsgPrintDN(Inv, DS);
                printfrm.ShowDialog();
            }
        }

        private void btnPrintPopupDN_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
                ISHCMC_Invoice Inv = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(LastRowHandleInvoice);
                MsgPrintDN printfrm = new MsgPrintDN(Inv, DS);
                printfrm.ShowDialog();
           
        }

        private void btnPrintProformInvST_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(LastRowHandleStudent);
            List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            ListStudent.Add(student);
            this.PrintProformaInv(ListStudent);
        }

        private void btnSelectAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView_Student.SelectAll();
        }

        private void btnSetDiscount_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(LastRowHandleStudent);
             MsgSetDiscount dis = new MsgSetDiscount(DS, common, student);
             dis.ShowDialog();
        }

        private void lookUpEdit_SchoolYear_EditValueChanged(object sender, EventArgs e)
        {
            string schoolyear = "";
            string school = "";
            if (this.lookUpEdit_SchoolYear.EditValue != null)
            {
                schoolyear = this.lookUpEdit_SchoolYear.EditValue.ToString();
            }
            if (this.lookUpEdit_School.EditValue != null)
            {
                school = this.lookUpEdit_School.EditValue.ToString();
            }
            var querySection = from Section in DS.ISHCMC_Sections
                               where Section.SchoolYear == schoolyear && Section.School == school
                               select Section;
            this.iSHCMCSectionBindingSource.DataSource = querySection;
            if (querySection.Count() > 0)
            {
                this.lookUpEdit_Section.EditValue = querySection.First().SectionCode;
            }
            
        }

        private void groupControl_Invoice_Paint(object sender, PaintEventArgs e)
        {

        }

        private void lookUpEdit_School_EditValueChanged(object sender, EventArgs e)
        {
            string schoolyear = "";
            string school = "";
            if (this.lookUpEdit_SchoolYear.EditValue != null)
            {
                schoolyear = this.lookUpEdit_SchoolYear.EditValue.ToString();
            }
            if (this.lookUpEdit_School.EditValue != null)
            {
                school = this.lookUpEdit_School.EditValue.ToString();
            }
            var querySection = from Section in DS.ISHCMC_Sections
                               where Section.SchoolYear == schoolyear && Section.School == school
                               select Section;
            this.iSHCMCSectionBindingSource.DataSource = querySection;
            if (querySection.Count() > 0)
            {
                this.lookUpEdit_Section.EditValue = querySection.First().SectionCode;
            }
            this.LoadData();
        }

        private void btnValidate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
           
        }

        private void btnFulfillment_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            foreach (int i in this.gridView_Student.GetSelectedRows())
            {
                ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                ListStudent.Add(itemStudent);
            }
            InvoiceFullfillmentOTHF itemInvFullfillment = new InvoiceFullfillmentOTHF(DS);
            itemInvFullfillment.LoadInvoice(ListStudent, "UnFulfilled");
        }

        private void btnPurchaseCOE_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ISHCMC_V_StudentListWithDebtor student = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(LastRowHandleStudent);
            DialogResult result = MessageBox.Show("Are you sure to make purchase for this student !!");
            if (result == DialogResult.OK)
            {
                int i = common.COEPurchase(student);
                if(i == 1)
                {

                    MessageBox.Show("This student purchased COE in school , after they make payment to us. system will allocated COE Number to student automatically");
                    this.LoadDataPerStudentRowHandle(LastRowHandleStudent);
                }
                else
                {
                    MessageBox.Show("System occured error , please contact administrator!");
                }
            }
        }

        private void btnUpdateDueDate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_Invoice> ListInvoiceDetail = new List<ISHCMC_Invoice>();
            ISHCMC_Invoice InvDetail = new ISHCMC_Invoice();
            foreach (int i in this.gridView_Invoice.GetSelectedRows())
            {
                InvDetail = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(i);
                ListInvoiceDetail.Add((ISHCMC_Invoice)this.gridView_Invoice.GetRow(i));
            }

            MsgUpdateDueDate DueDateFrm = new MsgUpdateDueDate(DS, ListInvoiceDetail);
            DueDateFrm.ShowIcon = false;
            DialogResult result = DueDateFrm.ShowDialog();
            if (result == DialogResult.Yes)
            {
                MessageBox.Show("Update due date sucessfully");

                this.LoadDataPerInvoiceHeader(InvDetail.InvoiceHeaderID.Value);
            }
            if (result == DialogResult.No)
            {
                MessageBox.Show("Occured error ,please contact administrator");

            }
          
        }

        private void cbxItem_EditValueChanged(object sender, EventArgs e)
        {
            var queryItems = from IT in DS.Items
                             where IT.ItemCode == this.cbxItem.EditValue.ToString()
                             select IT;
            this.txtPrice.EditValue = queryItems.First().SalesPackagePrice;
            this.txtItemDescription.EditValue = queryItems.First().Description;
           

        }

        private void btnFulfillmentInvoice_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            foreach (int i in this.gridView_Student.GetSelectedRows())
            {
                ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                ListStudent.Add(itemStudent);
            }
            List<ISHCMC_Invoice> ListInvoiceDetail = new List<ISHCMC_Invoice>();
            foreach (int j in this.gridView_Invoice.GetSelectedRows())
            {
                ISHCMC_Invoice itemInv = (ISHCMC_Invoice)this.gridView_Invoice.GetRow(j);
                ListInvoiceDetail.Add(itemInv);
            }
            InvoiceFullfillmentOTHF itemInvFullfillment = new InvoiceFullfillmentOTHF(DS);
            itemInvFullfillment.LoadInvoiceDetail(ListStudent, ListInvoiceDetail, "UnFulfilled");
        }

        private void btnLoadInvoice_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            List<ISHCMC_V_StudentListWithDebtor> ListStudent = new List<ISHCMC_V_StudentListWithDebtor>();
            foreach (int i in this.gridView_Student.GetSelectedRows())
            {
                ISHCMC_V_StudentListWithDebtor itemStudent = (ISHCMC_V_StudentListWithDebtor)this.gridView_Student.GetRow(i);
                ListStudent.Add(itemStudent);
            }
            this.LoadListInvoice(ListStudent);
        }

        

       

     

    
     
    }
}